package com.mwm.paiwei.meiye.widgets;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mwm.paiwei.meiye.inter.TitleBarClickListener;
import com.mwm.paiwei.meiye.R;
import com.mwm.paiwei.meiye.util.Util;

/**
 * Created by wujamie on 17/3/3.
 */

public class TitleBarView extends RelativeLayout {

    private String titleLeftText;      //左侧按钮文字
    private String titleRightText;     //右侧按钮文字
    private String titleCenterText;    //标题文字
    private int titleLeftColor;     //左侧标题文字颜色
    private int titleRightColor;    //右侧标题文字颜色
    private int titleCenterColor;   //标题文字颜色
    private Drawable titleLeftIcon;     //左侧按钮图标
    private Drawable titleRightIcon;    //右侧按钮图标
    private float titleLeftTextSize;     //左侧文字大小
    private float titleRightTextSize;    //标题文字大小
    private float titleCenterTextSize;   //右侧文字大小
    private boolean isShowTitleLeftIcon;        //是否显示左侧标题图标
    private boolean isShowTitleLeftText;        //是否显示左侧标题文字
    private boolean isShowTitleLeft;            //是否显示左侧标题
    private boolean isShowTitleRight;           //是否显示右侧标题
    private boolean isShowTitleRightIcon;       //是否显示右侧标题图标
    private boolean isShowTitleRightText;       //是否显示右侧标题文字
    private boolean isShowTitleCenterText;      //是否显示中间标题
    private float titleLeftIconWidth;       //左侧图标宽
    private float titleLeftIconHeight;      //左侧图标高
    private float titleRightIconWidth;      //右侧图标宽
    private float titleRightIconHeight;     //右侧图标高

    private LinearLayout llTitle;           //标题布局控件
    private RelativeLayout rlTitleLeft;     //左侧布局控件
    private TextView tvTitleLeft;           //左侧文本控件
    private ImageView ivTitleLeft;          //左侧图片控件
    private RelativeLayout rlTitleRight;    //右侧布局控件
    private TextView tvTitleRight;          //右侧文本控件
    private ImageView ivTitleRight;         //右侧图片控件
    private RelativeLayout rlTitleCenter;   //中间布局控件
    private TextView tvTitleCenter;         //中间文本控件
    private LinearLayout.LayoutParams llTitleParams;         //左侧布局控件布局参数
    private LinearLayout.LayoutParams rlTitleLeftParams;   //左侧布局控件布局参数
    private LinearLayout.LayoutParams rlTitleRightParams;  //右侧布局控件布局参数
    private LinearLayout.LayoutParams rlTitleCenterParams; //中间布局控件布局参数
    private LayoutParams tvTitleLeftParams;   //左侧文本控件布局参数
    private LayoutParams tvTitleRightParams;  //右侧文本控件布局参数
    private LayoutParams tvTitleCenterParams; //中间文本控件布局参数
    private LayoutParams ivTitleLeftParams;   //左侧图片控件布局参数
    private LayoutParams ivTitleRightParams;  //右侧图片控件布局参数
    public TitleBarClickListener listener;

    public TitleBarView(Context context) {
        super(context);
    }

    public TitleBarView(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.Title_bar);
        init(context, typedArray);
    }

    /**
     * 初始化参数，获取属性值
     *
     * @param context 上下文
     * @param ta
     */
    private void init(Context context, TypedArray ta) {
        titleLeftText = ta.getString(R.styleable.Title_bar_titleLeftText);
        titleRightText = ta.getString(R.styleable.Title_bar_titleRightText);
        titleCenterText = ta.getString(R.styleable.Title_bar_titleCenterText);
        titleLeftColor = ta.getColor(R.styleable.Title_bar_titleLeftColor, 0);
        titleRightColor = ta.getColor(R.styleable.Title_bar_titleRightColor, 0);
        titleCenterColor = ta.getColor(R.styleable.Title_bar_titleCenterColor, 10);
        titleLeftIcon = ta.getDrawable(R.styleable.Title_bar_titleLeftIcon);
        titleRightIcon = ta.getDrawable(R.styleable.Title_bar_titleRightIcon);
        titleLeftTextSize = ta.getDimension(R.styleable.Title_bar_titleLeftTextSize, 8);
        titleRightTextSize = ta.getDimension(R.styleable.Title_bar_titleRightTextSize, 8);
        titleCenterTextSize = ta.getDimension(R.styleable.Title_bar_titleCenterTextSize, 10);
        isShowTitleLeftIcon = ta.getBoolean(R.styleable.Title_bar_isShowTitleLeftIcon, true);
        isShowTitleLeftText = ta.getBoolean(R.styleable.Title_bar_isShowTitleLeftText, true);
        isShowTitleLeft = ta.getBoolean(R.styleable.Title_bar_isShowTitleLeft, true);
        isShowTitleRight = ta.getBoolean(R.styleable.Title_bar_isShowTitleRight, true);
        isShowTitleRightIcon = ta.getBoolean(R.styleable.Title_bar_isShowTitleRightIcon, true);
        isShowTitleRightText = ta.getBoolean(R.styleable.Title_bar_isShowTitleRightText, true);
        isShowTitleCenterText = ta.getBoolean(R.styleable.Title_bar_isShowTitleCenterText, true);
        ta.recycle();

        llTitle = new LinearLayout(context);
        rlTitleLeft = new RelativeLayout(context);
        tvTitleLeft = new TextView(context);
        ivTitleLeft = new ImageView(context);
        rlTitleRight = new RelativeLayout(context);
        tvTitleRight = new TextView(context);
        ivTitleRight = new ImageView(context);
        rlTitleCenter = new RelativeLayout(context);
        tvTitleCenter = new TextView(context);

        llTitleParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, Util.dip2px(context, 45));
        addView(llTitle, llTitleParams);
        llTitle.setOrientation(LinearLayout.HORIZONTAL);

        rlTitleLeftParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, 2);
        llTitle.addView(rlTitleLeft, rlTitleLeftParams);

        if (isShowTitleLeft) {
            rlTitleLeft.setVisibility(View.VISIBLE);
            if (isShowTitleLeftIcon) {
                ivTitleLeft.setVisibility(View.VISIBLE);

                ivTitleLeft.setBackground(titleLeftIcon);
            } else {
                ivTitleLeft.setVisibility(View.GONE);
            }
            if (isShowTitleLeftText) {
                tvTitleLeft.setVisibility(View.VISIBLE);

                tvTitleLeft.setText(titleLeftText);
                tvTitleLeft.setTextColor(titleLeftColor);
                tvTitleLeft.setTextSize(titleLeftTextSize);
            } else {
                tvTitleLeft.setVisibility(View.GONE);
            }
        } else {
            rlTitleLeft.setVisibility(View.GONE);
        }

        rlTitleRightParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, 2);
        llTitle.addView(rlTitleRight, rlTitleRightParams);

        if (isShowTitleRight) {
            rlTitleRight.setVisibility(View.VISIBLE);
            if (isShowTitleRightIcon) {
                ivTitleRight.setVisibility(View.VISIBLE);

                ivTitleRight.setBackground(titleRightIcon);
            } else {
                ivTitleRight.setVisibility(View.GONE);
            }
            if (isShowTitleRightText) {
                tvTitleRight.setVisibility(View.VISIBLE);

                tvTitleRight.setText(titleRightText);
                tvTitleRight.setTextColor(titleRightColor);
                tvTitleRight.setTextSize(titleRightTextSize);
            } else {
                tvTitleRight.setVisibility(View.GONE);
            }
        } else {
            rlTitleRight.setVisibility(View.GONE);
        }

        rlTitleCenterParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, 1);
        llTitle.addView(rlTitleCenter, rlTitleCenterParams);

        if (isShowTitleCenterText) {
            rlTitleCenter.setVisibility(View.VISIBLE);

            tvTitleCenterParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            tvTitleCenterParams.addRule(RelativeLayout.CENTER_IN_PARENT, TRUE);
            tvTitleCenter.setText(titleCenterText);
            tvTitleCenter.setTextColor(titleCenterColor);
            tvTitleCenter.setTextSize(titleCenterTextSize);
            rlTitleCenter.addView(tvTitleCenter, tvTitleCenterParams);
        } else {
            rlTitleCenter.setVisibility(View.GONE);
        }

        rlTitleLeft.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.leftClick();
            }
        });
        rlTitleRight.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.rightClick();
            }
        });
    }

    /**
     * 通过接口来获得回调者对接口的实现
     *
     * @param listener
     */
    public void setOnTitleBarClickListener(TitleBarClickListener listener) {
        this.listener = listener;
    }


    /**
     * 设置左侧按钮是否可见
     *
     * @param flag 是否可见
     */
    public void setLeftBtnVisable(boolean flag) {
        if (flag) {
            rlTitleLeft.setVisibility(VISIBLE);
        } else {
            rlTitleLeft.setVisibility(GONE);
        }
    }

    /**
     * 设置右侧按钮是否可见
     *
     * @param flag 是否可见
     */
    public void setRightBtnVisable(boolean flag) {
        if (flag) {
            rlTitleRight.setVisibility(VISIBLE);
        } else {
            rlTitleRight.setVisibility(GONE);
        }
    }
}
